<template>
    <div class="todo-footer" v-show="total > 0">
        <label>
            <input type="checkbox" :checked="completeAll" @click="doOrUndo"/>
        </label>
        <span>
          <span>已完成{{finishedTotal}}</span> / 全部{{total}}
        </span>

        <button  class="btn btn-danger" @click="rmDone">清除已完成任务</button>
    </div>

</template>

<script>
export default {
    name:"MyFooter",
    data(){
        return {
        }
    },
    props: ["todos"],
    methods:{
        doOrUndo:function(event){
            if(event.target.checked){
              this.todos.forEach((item)=>{
                  item.done = true;
              })
            }else {
              this.todos.forEach((item)=>{
                  item.done = false;
              })
            }
        },
        rmDone: function(){
            const arr = this.todos.filter((item)=>{
                return item.done;
            })
            for (let i = 0; i < arr.length; i++) {
                this.todos.splice(this.todos.indexOf(arr[i]), 1);
            }
        }
    },
    computed:{
        finishedTotal(){
            const arr = this.todos.filter(item=>{
                if(item.done){
                    return true;
                }
            })
            console.log(111,  arr.length)
            // 统计 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
           const res = this.todos.reduce((res,item)=>{
               return res+(item.done?1:0)
            },0);
            console.log(222,  res)
            return res;

            // return arr.length;
        },
        total(){
            return this.todos.length;
        },
        completeAll(){
            if(this.finishedTotal === this.total){
                return true;
            }else{
                return  false;
            }
        }
    }
}
</script>
<style scoped>

/*footer*/
.todo-footer {
    height: 40px;
    line-height: 40px;
    padding-left: 6px;
    margin-top: 5px;
}

.todo-footer label {
    display: inline-block;
    margin-right: 20px;
    cursor: pointer;
}

.todo-footer label input {
    position: relative;
    top: -1px;
    vertical-align: middle;
    margin-right: 5px;
}

.todo-footer button {
    float: right;
    margin-top: 5px;
}
</style>

